package tcpfileupadatedemo1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by L.jp
 * Description:
 * User: 86189
 * Date: 2022-08-09
 * Time: 17:40
 */

public class ServerDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket  serverSocket=new ServerSocket(1000);
    
        while (true) {
            //别忘记了建立了建立来链接
            Socket accept=serverSocket.accept();
            //网络中的流,从客户端读取数据的
            BufferedInputStream bis=new BufferedInputStream(accept.getInputStream());
            //本地的流，写到本地，永久存储
            BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("src/ServerImg/copy.jpg"));
            //从网络流中读取到本地
            int b;
            while ((b=bis.read())!=-1){
                //写到本地的输出流
                bos.write(b);
            }
        
            //向客户端发送“上传成功”
            BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bw.write("上传成功");
            bw.newLine();
            bw.flush();
            //关闭本地链接
            bos.close();
            accept.close();
        }
    }
}
